package com.ethan.cj.ssm.service.impl;

import com.ethan.cj.ssm.dao.FirstTrialDao;
import com.ethan.cj.ssm.entity.Credit;
import com.ethan.cj.ssm.entity.CreditGranting;
import com.ethan.cj.ssm.entity.Product;
import com.ethan.cj.ssm.service.CreditGrantingService;
import com.ethan.cj.ssm.service.FirstTrialService;
import com.ethan.cj.ssm.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 初审逻辑处理层接口的实现
 */
@Service
public class FirstTrialServiceImpl implements FirstTrialService {
    /**
     * 初审dao层映射
     */
    @Autowired
    private FirstTrialDao ftd;

    /**
     * 授信机构dao层映射
     */
    @Autowired
    private CreditGrantingService cgs;

    /**
     * 授信产品dao层映射
     */
    @Autowired
    private ProductService ps;

    /**
     * 根据id查询贷款信息
     *
     * @param cid 查询条件
     * @return 贷款信息对象
     */
    @Override
    public Credit selectCreditByCid(int cid) {
        return ftd.selectCreditByCid(cid);
    }

    /**
     * 修改状态
     *
     * @param cid    修改对象的ID
     * @param cstate 修改之后的状态
     * @return 影响行数
     */
    @Override
    public Map<Object, Object> updateFirstTrial(int cid, int cgid, int pid, int cstate) {
        int state = -1;
        int i = 0;
        CreditGranting cg = new CreditGranting();
        Product p = new Product();
        Map<Object, Object> result = new HashMap<>();
        if (cgid != 0) {
            cg = cgs.selecteByCgid(cgid);
        }
        if (pid != 0) {
            p = ps.selectByPid(pid);
        }
        if (cstate == 1) {
            state = 2;
            i = ftd.updateFirstTrial(cid, cg.getCgname(), p.getPname(), state);
            if (i > 0) {
                result.put("code", 1);
                result.put("msg", "初审成功,你选择了审核通过！");
            } else {
                result.put("code", 0);
                result.put("msg", "初审失败");
            }
        } else if (cstate == 2) {
            state = 6;
            i = ftd.updateFirstTrial(cid, cg.getCgname(), p.getPname(), state);
            if (i > 0) {
                result.put("code", 1);
                result.put("msg", "初审成功,你选择了审核拒绝！");
            } else {
                result.put("code", 0);
                result.put("msg", "初审失败");
            }
        }
        return result;
    }
}
